package com.killer.auth.service.controller;

import com.killer.common.entity.Result;
import com.killer.auth.service.IndexService;
import com.killer.auth.util.EsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private EsUtil esUtil;

    @RequestMapping("/importAll")
    public Result importAll() {
        String indexName = indexService.importAll();
        //查询该别名对应的索引
        String[] oldIndexs = esUtil.getAliasIndex("goods");
        log.info("旧索引名如下:{}", Arrays.toString(oldIndexs));
        //将别名更换至新索引名
        boolean result=esUtil.changeAliasAfterReindex("goods", indexName,oldIndexs);
        if (result) {
            result=esUtil.deleteIndex(oldIndexs);
        }
        return result?Result.success("重建索引成功"):Result.fail("重建索引失败");
    }

}
